package com.qcs.sleep.warn.action;

import com.qcs.sleep.call.AppCaller;
import com.qcs.sleep.call.AppWatcher;

import java.util.Observable;
import java.util.Observer;

/**
 * Created by chshqiang on 15-7-21.
 * 护眼模式
 */
public abstract class RunAppWatcher extends AppWatcher {
    public static final int TIP_COUNT = 3;
    public static final int TIP_DURATION = 7;

    private int duration;
    private int tipCount;
    private int tipDuration;

    private int level;

    public RunAppWatcher() {
        duration = 0;
        tipDuration = 0;
        tipCount = 0;
        level = 0;
    }

    /**
     * update
     *
     * @see Observer#update(Observable, Object)
     */
    @Override
    public void update(Observable observable, Object data) {
        if (data instanceof AppCaller)
            update((AppCaller) data);
    }

    public void update(AppCaller caller) {
        if (isTiming(caller.getPackageName())) {
            duration++;

            if (isMaxLevel(level) && getLevelDuration(level) <= duration && (duration - tipDuration) >= TIP_DURATION && tipCount <= level * TIP_COUNT) {
                tipDuration = duration;
                tipCount++;

                onWarn(level);
            }
        } else {
            duration = 0;
        }

    }

    protected abstract boolean isTiming(String packageName);

    protected abstract boolean isMaxLevel(int level);

    protected abstract int getLevelDuration(int level);

    protected abstract void onWarn(int level);
}
